//存放主要交互逻辑代码

console.log(path);
var seckill = {
	URL:{
		now:function(){
			return path+'/seckill/time/now'
		},
		exposer:function(seckillId){
			return path +'/seckill/'+seckillId+'/exposer';
		},
		execution:function(seckillId,md5){
			return path+ '/seckill/'+seckillId+'/'+md5+'/execution';
		}
	},
	handlerSeckill:function(seckillId, seckillBox){	//执行秒杀逻辑
		seckillBox.hide()
		.html('<button class="btn btn-primary btn-lg" id="killBtn">开始秒杀</button>');
		$.post(seckill.URL.exposer(seckillId),function(response){
			if(response && response.success){
				var exposer = response.data;
				if(exposer.exposed){
					$('.glyphicon-time').hide();
					var killUrl = seckill.URL.execution(seckillId,exposer.md5);
					$('#killBtn').one('click',function(){
						$(this).addClass('disabled');
						//发送秒杀的请求
						$.post(killUrl,{},function(response){
							console.log(response);
							if(response && response.success){
								var result = response.data;
								var state = result.state;
								var stateInfo = result.stateInfo;
								seckillBox.html('<span class="label label-success">'+stateInfo+'</span>');
							};
						});
					});
					seckillBox.show();
				}else{
					var now = exposer.now;
					var start = exposer.start;
					var end = exposer.end;
					seckill.countdown(seckillId,now,start,end);
				}
			};
		});
	},
	validatePhone:function(phone){
		if(phone && (phone.length == 11) && !isNaN(phone)){
			return true;
		}else{
			return false;
		}
	},
	countdown:function(seckillId,nowTime,startTime,endTime){
		 var seckillBox = $('#seckill-box');
		if(nowTime > endTime){
			 seckillBox.html('秒杀结束!');
		}else if(nowTime < startTime){
			 //秒杀未开始,计时事件绑定
            var killTime = new Date(startTime + 1000);//todo 防止时间偏移
            seckillBox.countdown(killTime, function (event) {
                //时间格式
                var format = event.strftime('秒杀倒计时: %D天 %H时 %M分 %S秒 ');
                seckillBox.html(format);
            }).on('finish.countdown', function () {
                //时间完成后回调事件
                //获取秒杀地址,控制现实逻辑,执行秒杀
                console.log('______fininsh.countdown');
                seckill.handlerSeckill(seckillId, seckillBox);
           });
		}else{
			 seckill.handlerSeckill(seckillId, seckillBox);
		};
	},
	//详情页秒杀逻辑
	detail:{
		//详情页初始化
		init:function(params){
			//手机验证和登陆，计时交互
			//规划交互流程
			var killPhone = $.cookie('killPhone');
			//验证手机号
			if(!seckill.validatePhone(killPhone)){	//没有获取手机号
				//绑定phone
				var killPhoneModal = $('#killPhoneModal');
				killPhoneModal.modal({
					show:true,
					backdrop:'static',
					keyboard:false,
				});
				$('#killPhoneBtn').click(function(){
					var inputPhone = $('#killPhoneKey').val();
					if(seckill.validatePhone(inputPhone)){
						$.cookie('killPhone',inputPhone,{expires:7,path:'/mvc2/'});
						location.reload();
					}else{
						$('#killPhoneMessage').hide().html('手机号码错误').show(300);
					}
				});
			};
			//已经登陆
			//计时交互
			var startTime = params['startTime'];
			var endTime = params['endTime'];
			var seckillId = params['seckillId'];
			$.get(seckill.URL.now(),{},function(response){
				console.log(response);
				if(!!response && response){
					var nowTime = response.data;
					seckill.countdown(seckillId,nowTime,startTime,endTime);
				}else{
//					doerror
				};
			})
		}
	}
};
